﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Pop3;
using System.Net.Mail;

namespace TPTecnicas.MessageProcessor.Common
{
	public class Utils
	{
		public static string SaveMailAttachment(System.Net.Mail.Attachment attachment, string destinationFilePath)
		{
			byte[] allBytes = new byte[attachment.ContentStream.Length];
			int bytesRead = attachment.ContentStream.Read(allBytes, 0, (int)attachment.ContentStream.Length);

			FileInfo file = new System.IO.FileInfo(destinationFilePath);
			file.Directory.Create();

			destinationFilePath += attachment.Name;
			BinaryWriter writer = new BinaryWriter(new FileStream(destinationFilePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None));
			writer.Write(allBytes);
			writer.Close();

			return (destinationFilePath);
		}


		public static Message RxMessageToMessage(Pop3.RxMailMessage rxMail)
		{
			Message mail = new Message();
			List<string> attachments = new List<string>();

			mail.Sender = rxMail.From.Address.ToString();
			mail.Destinatary = rxMail.DeliveredTo.ToString();
			mail.Subject = rxMail.Subject.ToString();			

			if (!String.IsNullOrEmpty(rxMail.Body))
				mail.Body = rxMail.Body;
			else if ((rxMail.Entities.Count > 0) &&(!String.IsNullOrEmpty(rxMail.Entities[0].Body)))
				mail.Body = rxMail.Entities[0].Body;
			else if ((rxMail.Entities.Count > 1) && (!String.IsNullOrEmpty(rxMail.Entities[1].Body)))
				mail.Body = rxMail.Entities[1].Body;
			else
				mail.Body = String.Empty;

			string destinationFilePath = mail.Sender + "/" + Guid.NewGuid() + Constants.AttachmentGuiSenderSeparator;
			if (rxMail.Attachments.Count > 0)
			{
				foreach (var attachment in rxMail.Attachments)
				{					
					attachments.Add(SaveMailAttachment(attachment, destinationFilePath));
				}
			}
			else if ((rxMail.Entities.Count > 1) && (rxMail.Entities[0].Attachments.Count > 0))
			{
				foreach (var attachment in rxMail.Entities[0].Attachments)
				{					
					attachments.Add(SaveMailAttachment(attachment, destinationFilePath));
				}
			}
			else if ((rxMail.Entities.Count > 1) && (rxMail.Entities[1].Attachments.Count > 0))
			{
				foreach (var attachment in rxMail.Entities[1].Attachments)
				{
					attachments.Add(SaveMailAttachment(attachment, destinationFilePath));
				}
			}
			
			mail.Attachments= attachments;
			return mail;
		}



		public static void Log(String message)
		{
			DateTime datet = DateTime.Now;
			//String filePath = "C:/log" + datet.ToString("MM_dd") + ".log";
			String filePath = "C:/log.log";
			if (!File.Exists(filePath))
			{
				FileStream files = File.Create(filePath);
				files.Close();
			}
			try
			{
				StreamWriter sw = File.AppendText(filePath);
				sw.WriteLine(datet.ToString("dd/MM hh:mm:ss:mm") + "> " + message);
				sw.Flush();
				sw.Close();
			}
			catch (Exception e)
			{
				Console.WriteLine(e.Message.ToString());
			}
		}

	}
}
